package com.yishan.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yishan.dao.CompanyDao;
import com.yishan.domain.Company;
import com.yishan.service.CompanyService;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Future;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

/**
 * @Author yishan
 * @Date 2021/4/5 0005 12:56
 * @Version 1.0
 */
@Service
public class CompanyServiceImpl implements CompanyService {

	@Autowired
	private CompanyDao companyDao;

	@Override
	public void save(Company company) {

		// id使用UUID的生成策略来获取
		String id = UUID.randomUUID().toString().replace("-","").substring(0,15);
		company.setId(id);

		//联系方式进行 - - 拼接
		StringBuffer phone = new StringBuffer(company.getPhone());
		phone.insert(3,"-");
		phone.insert(8,"-");
		company.setPhone(phone.toString());

		companyDao.save(company);

	}

	@Override
	public void delete(String id) {

		companyDao.delete(id);

	}

	@Override
	public void update(Company company) {

		//联系方式进行 - - 拼接
		StringBuffer phone = new StringBuffer(company.getPhone());
		phone.insert(3, "-");
		phone.insert(8, "-");
		company.setPhone(phone.toString());

		companyDao.update(company);

	}

	@Override
	public Company findById(String id) {

		return companyDao.findById(id);

	}

	@Override
	public List<Company> findAll() {
		System.out.println(Thread.currentThread().getName());
		return companyDao.findAll();

	}


	// @Async
	// public Future<List<Company>> getAll() {
	// 	System.out.println(Thread.currentThread().getName());
	// 	List<Company> list = companyDao.findAll();
	// 	return CompletableFuture.completedFuture(list);
	// }

	@Override
	public PageInfo findAll(int page, int size) {

		PageHelper.startPage(page, size);
		List<Company> list = companyDao.findAll();
		return new PageInfo(list);

	}

	@Override
	public void deleteMany(String ids) {
		String[] idss = ids.split(",");
		for (String s : idss) {
			delete(s);
		}
	}

	// @Override
	// public Future<Void> save(Company company) {
	// 	System.out.println(Thread.currentThread().getName());
	// 	// id使用UUID的生成策略来获取
	// 	String id = UUID.randomUUID().toString().replace("-", "").substring(0, 15);
	// 	company.setId(id);
	//
	// 	//联系方式进行 - - 拼接
	// 	StringBuffer phone = new StringBuffer(company.getPhone());
	// 	phone.insert(3, "-");
	// 	phone.insert(8, "-");
	// 	company.setPhone(phone.toString());
	//
	// 	int result = companyDao.save(company);
	// 	return CompletableFuture.completedFuture(null);
	// }
}
